//
//  BFSendCodeViewController.m
//  BFoodSystem
//
//  Created by 浙江择富 on 2018/4/4.
//  Copyright © 2018年 陈名正. All rights reserved.
//

#import "BFSendCodeViewController.h"
#import "BFLoginServices.h"
#import "BFSetPasswordViewController.h"

@interface BFSendCodeViewController ()

@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, assign) NSInteger timeCount;
@property (nonatomic, assign) NSInteger begainTime;

@end

@implementation BFSendCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.userNameLab.text = self.nameStr;
    [self sendSMSRequestWithName:self.userNameLab.text];

}
- (IBAction)getCode:(id)sender {
    
 [self sendSMSRequestWithName:self.userNameLab.text];
    
}


- (IBAction)nextBtn:(id)sender {
    if(!self.smsCodeTF.text.length){
        [BFUtils showAlertController:1 title:@"" message:@"请输入验证码"];
        return;
    }
    
    BFSetPasswordViewController *setPasswordVC = [BFSetPasswordViewController new];
    setPasswordVC.userNameStr = self.userNameLab.text;
    setPasswordVC.inviteStr = self.smsCodeTF.text;
    setPasswordVC.inviteStr = self.inviteStr;
    setPasswordVC.selfType = self.selfType;
    
    [self.navigationController pushViewController:setPasswordVC animated:YES];
    
    
}

- (void)sendSMSRequestWithName:(NSString *)nameStr{
    
    NSString *codeType = @"register";
    
    if ([self.selfType isEqualToString:@"changPsd"]) {
        
        codeType = @"findPass";
    }
    [BFUtils showProgressHUDWithTitle:@"发送中...." inView:self.view animated:YES];
    
    [[BFLoginServices alloc] getSMSCodeWithPhoneNum:nameStr sendType:codeType SuccessBlock:^(id result) {
        
        [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
        [BFUtils showAlertController:0 title:@"" message:@"发送成功"];
        self.begainTime = [[NSDate date] timeIntervalSince1970];
        self.timeCount = 60;
        _timer =  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
         self.sendCodeBtn.enabled = NO;
       
        
    } errorCode:^(NSInteger errorCode, NSString *errorMessage) {
        [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
        [BFUtils showAlertController:0 title:@"" message:errorMessage];
    } Failure:^(NSError *error) {
        [BFUtils hideProgressHUDInView:self.view delegate:self animated:YES afterDelay:0];
        [BFUtils showAlertController:0 title:@"" message:@"网络错误"];
    }];
}

- (void)timeChange
{
    // 倒计时
    if (self.timeCount == 0){
        [self timeComple];
     self.sendCodeBtn.enabled = YES;
        return;
    }
    self.timeCount = 60 - (NSInteger)([[NSDate date] timeIntervalSince1970] - self.begainTime);
        [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"%lds",(long)self.timeCount] forState:UIControlStateNormal];
     [self.sendCodeBtn setTitleColor:rgb(157,157,157) forState:UIControlStateNormal];
}


- (void)timeComple
{
    [_timer invalidate];
    [self.sendCodeBtn setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
    [self.sendCodeBtn setTitleColor:rgb(243,85,15) forState:UIControlStateNormal];
    self.timeCount = 61;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
